﻿using ESApi.ElasticSearch;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ESApi.Controllers
{
    [RoutePrefix("api/es")]
    public class ESController : ApiController
    {
        /// <summary>
        /// 创建映射
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("mapping")]
        public IHttpActionResult Mapping()
        {
            return Ok(ESHelper.CreateMapping());

        }

        /// <summary>
        /// 添加一个文档
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        [HttpPost, Route("add")]
        public IHttpActionResult Add(Article article)
        {
            return Ok(ESHelper.CreateArticle(article));

        }
        /// <summary>
        /// 批量添加文档
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>

        [HttpPost, Route("addBulk")]
        public IHttpActionResult addBulk(List<Article> list)
        {
            return Ok(ESHelper.CreateBulk(list));

        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost, Route("delete")]
        public IHttpActionResult Delete(List<long> ids)
        {
            //根据id获取list
            var list = ESHelper.GetByIds(ids);
            return Ok(ESHelper.DeleteBulk(list));

        }

        /// <summary>
        /// 全文检索
        /// </summary>
        /// <param name="page"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpGet, Route("search")]
        public IHttpActionResult Search(int? page,string keyword)
        {
           int pageIndex = page ?? 1;
            return Ok(ESHelper.Search(pageIndex, keyword));

        }
    }
}
